package com.bigknow.appstore.web.console.service.impl;

import com.bigknow.appstore.common.constants.DateFormatConstants;
import com.bigknow.appstore.proxy.cache.impl.EhCacheManager;
import com.bigknow.appstore.proxy.entity.ProxyMeta;
import com.bigknow.appstore.proxy.entity.ProxyResourceMeta;
import com.bigknow.appstore.web.base.entity.User;
import com.bigknow.appstore.web.base.service.IFusionService;
import com.bigknow.appstore.web.console.app.*;
import com.bigknow.appstore.web.console.dao.IApplicationDao;
import com.bigknow.appstore.web.console.dao.ISubscriberDao;
import com.bigknow.appstore.web.console.service.IApplicationService;
import com.bigknow.appstore.web.console.service.ISubscriberService;
import com.bigknow.appstore.web.framework.service.BaseCRUDService;
import com.bigknow.appstore.web.util.PinyinUtils;
import com.bigknow.fusion.common.inf.server.appstore.FusionHandler;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.tools.ant.util.DateUtils;
import org.bson.types.ObjectId;
import org.mongodb.morphia.Datastore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by 李科风 on 2016-09-22.
 */
@Service
public class SubscriberService extends BaseCRUDService<Subscriber, ISubscriberDao> implements ISubscriberService {
    @Autowired
    private Datastore ds;

    @Autowired
    private IFusionService fusionService;

    @Override
    public Subscriber getSub(String id, SubscribeState state) {
        return ds.find(Subscriber.class).filter("id", id).filter("state", state).get();
    }

    @Override
    public void reply(Subscriber sub) {
        Subscriber _sub = ds.find(Subscriber.class).filter("id", sub.getId()).filter("state", SubscribeState.APPLYING).get();
        String userId = ((com.bigknow.appstore.web.base.entity.User) SecurityUtils.getSubject().getPrincipal()).getId();
        List<String> dataNodeIds = _sub.getDataNodeIds();
        int subscribeCount = _sub.getApplication().getSubscribeCount();
        if (SubscribeState.SUCCESS == sub.getState()) {
            FusionHandler handler = fusionService.getFusionHandler();
            for (String id : _sub.getApplication().getServiceIds()) {
                //调用fusion接口服务授权
                handler.applyDvResourceServer(userId, id);
                //发送数据节点申请
                handler.sendDataNodeApply(dataNodeIds, id, "应用商店平台管理员申请数据节点访问权限，服务ID：[" + id + "],数据节点ID：[" + dataNodeIds + "]", userId);
            }
            ds.update(_sub.getApplication(), ds.createUpdateOperations(Application.class).set("subscribeCount", subscribeCount + 1));
        }
        ds.update(_sub, ds.createUpdateOperations(Subscriber.class).set("replyUserId", userId).set("state", sub.getState()).set("replyDesc", sub.getReplyDesc()).set("replyTime", DateUtils.format(new Date().getTime(), com.bigknow.appstore.auth.infrastructure.DateUtils.DATE_FORMAT)));
    }

}
